# 中序遍历
# 中序遍历是先遍历左子树，然后访问根节点，然后遍历右子树。


# 例子
"""
给定一个二叉树，返回它的中序 遍历。

示例:

输入: [1,null,2,3]
   1
    \
     2
    /
   3

输出: [1,3,2]

作者：力扣 (LeetCode)
链接：https://leetcode-cn.com/leetbook/read/data-structure-binary-tree/xecaj6/
来源：力扣（LeetCode）
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
"""


class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def inorderTraversal(self, root: TreeNode):  # Definition for a binary tree node.
        r = []
        if root is not None:
            r.extend(self.inorderTraversal(root.left))
            r.extend([root.val])
            r.extend(self.inorderTraversal(root.right))
        return r

    def inorder_traversal(self, root: TreeNode):
        values = lambda x: [] if not x else values(x.left) + [x.val] + values(x.right)
        return values(root)


if __name__ == '__main__':
    tree = TreeNode(3)
    tree.left = TreeNode(9)
    tree.right = TreeNode(20)
    tree.left.left = TreeNode(7)
    tree.left.right = TreeNode(8)
    tree.left.right.right = TreeNode(11)
    tree.right.left = TreeNode(15)
    tree.right.right = TreeNode(7)
    tree1 = TreeNode(2)
    tree1.left = TreeNode(1)
    tree1.right = TreeNode(3)
    tree2 = TreeNode(0)
    tree2.left = TreeNode(-3)
    tree2.left.left = TreeNode(-10)
    tree2.right = TreeNode(9)
    tree2.right.left = TreeNode(5)
    s = Solution()
    print(s.inorderTraversal(tree))
    print(s.inorder_traversal(tree))
    print(s.inorderTraversal(tree1))
    print(s.inorder_traversal(tree1))
    print(s.inorder_traversal(tree2))
